package com.stm.bi.model;

import com.stm.bi.dto.query.GroupField;
import com.stm.bi.enums.DateType;
import com.stm.bi.enums.GroupType;
import com.stm.bi.enums.ProductStructIndex;
import com.stm.saas.enums.ProductStructType;

import java.math.BigDecimal;
import java.util.*;

import static com.stm.saas.constant.IndexConstants.*;

/**
 * 简介
 *
 * @author zhouyuanpeng
 * @date 2024-04-29 10:25
 */
public class ProductStructureParam {

    public final static String SALES_FIELD_NAME = "sales_amount";
    public final static String QTY_FIELD_NAME = "qty";
    public final static String TOTAL_QTY_FIELD_NAME = "total_qty";
    public final static String RETURN_QTY_FIELD_NAME = "return_qty";
    public final static String RETURN_RATE_FIELD_NAME = "return_rate";
    public final static List<String> categoryList = Arrays.asList(ProductStructType.LEVEL1.getValue(), ProductStructType.LEVEL2.getValue(), ProductStructType.LEVEL3.getValue(), ProductStructType.LEVEL4.getValue(), ProductStructType.LEVEL5.getValue(), ProductStructType.LEVEL6.getValue(), ProductStructType.LEVEL7.getValue());

    private String productStructType;
    private ProductStructIndex productStructIndex;
    private DateType dateType;
    private String fieldName;
    private String totalName;
    private BigDecimal defaultValue = BigDecimal.ZERO;
    private Integer scaleNum = 2;
    private BigDecimal coefficient = BigDecimal.ONE;
    private String unit = "";
    private String typeName;
    private String businessField;
    private String businessFieldName;
    // 是否补充年初开始的数
    private Boolean needCumulative = Boolean.FALSE;
    // 是否需要补全日期
    private Boolean needComplete = Boolean.FALSE;

    List<GroupField> groupFields = new ArrayList<>();
    List<String> codes = new ArrayList<>();
    List<String> groupBy = new ArrayList<>();

    Map<String, Object> otherConditions = new HashMap<>();

    public ProductStructureParam(String productStructType, ProductStructIndex productStructIndex, DateType dateType) {
        this.productStructType = productStructType;
        this.productStructIndex = productStructIndex;
        this.dateType = dateType;

        this.groupBy.add("date");

        if (categoryList.contains(productStructType)) {
            this.typeName = ProductStructType.getFieldType(productStructType).getDesc();
            this.groupBy.add("policy_id");
            this.groupBy.add("policy_name");

            this.businessField = "policy_id";
            this.businessFieldName = "policy_name";

            otherConditions.put("level", productStructType);

        } else if (ProductStructType.STYLE.getValue().equals(productStructType)) {
            this.typeName = ProductStructType.STYLE.getDesc();
            this.groupBy.add("style_code");
            this.groupBy.add("style_name");

            this.businessField = "style_code";
            this.businessFieldName = "style_name";
        } else {
            this.typeName = ProductStructType.COLOR.getDesc();
            this.groupBy.add("prod_code");
            this.groupBy.add("prod_name");
            this.groupBy.add("size_code");
            this.groupBy.add("color_code");

            this.businessField = "prod_code";
            this.businessFieldName = "prod_name";
        }

        if (ProductStructIndex.RETURN_RATE.equals(productStructIndex)) {
            // 退货率
            this.fieldName = RETURN_RATE_FIELD_NAME;
            this.groupFields.add(new GroupField().setFieldName(QTY_FIELD_NAME).setGroupType(GroupType.SUM.getValue()));
            this.groupFields.add(new GroupField().setFieldName(TOTAL_QTY_FIELD_NAME).setGroupType(GroupType.SUM.getValue()));
            this.groupFields.add(new GroupField().setFieldName(RETURN_QTY_FIELD_NAME).setGroupType(GroupType.SUM.getValue()));
            this.totalName = "累计";
            this.coefficient = BigDecimal.valueOf(100);
            this.unit = "%";
            this.needCumulative = Boolean.TRUE;
            this.needComplete = Boolean.TRUE;
        } else if (ProductStructIndex.DIGESTIBILITY.equals(productStructIndex)) {
            // 售罄率
            this.fieldName = QTY_FIELD_NAME;
            this.groupFields.add(new GroupField().setFieldName(fieldName).setGroupType(GroupType.SUM.getValue()));
            this.totalName = "累计";
            this.coefficient = BigDecimal.valueOf(100);
            this.unit = "%";
            this.needCumulative = Boolean.TRUE;
            this.needComplete = Boolean.TRUE;
        } else if (ProductStructIndex.SALES_AMOUNT.equals(productStructIndex)) {
            // 销售额
            this.fieldName = SALES_FIELD_NAME;
            this.groupFields.add(new GroupField().setFieldName(fieldName).setGroupType(GroupType.SUM.getValue()));
            this.totalName = "合计(元)";
        } else {
            // 退货量
            this.fieldName = RETURN_QTY_FIELD_NAME;
            this.groupFields.add(new GroupField().setFieldName(fieldName).setGroupType(GroupType.SUM.getValue()));
            this.totalName = "合计";
            this.scaleNum = 0;
        }
    }

    public Map<String, Object> getOtherConditions() {
        return otherConditions;
    }

    public ProductStructureParam setOtherConditions(Map<String, Object> otherConditions) {
        this.otherConditions = otherConditions;
        return this;
    }

    public String getBusinessFieldName() {
        return businessFieldName;
    }

    public ProductStructureParam setBusinessFieldName(String businessFieldName) {
        this.businessFieldName = businessFieldName;
        return this;
    }

    public String getBusinessField() {
        return businessField;
    }

    public ProductStructureParam setBusinessField(String businessField) {
        this.businessField = businessField;
        return this;
    }

    public List<String> getGroupBy() {
        return groupBy;
    }

    public ProductStructureParam setGroupBy(List<String> groupBy) {
        this.groupBy = groupBy;
        return this;
    }

    public Boolean getNeedCumulative() {
        return needCumulative;
    }

    public ProductStructureParam setNeedCumulative(Boolean needCumulative) {
        this.needCumulative = needCumulative;
        return this;
    }

    public Boolean getNeedComplete() {
        return needComplete;
    }

    public ProductStructureParam setNeedComplete(Boolean needComplete) {
        this.needComplete = needComplete;
        return this;
    }

    public BigDecimal getCoefficient() {
        return coefficient;
    }

    public ProductStructureParam setCoefficient(BigDecimal coefficient) {
        this.coefficient = coefficient;
        return this;
    }

    public String getUnit() {
        return unit;
    }

    public ProductStructureParam setUnit(String unit) {
        this.unit = unit;
        return this;
    }

    public DateType getDateType() {
        return dateType;
    }

    public ProductStructureParam setDateType(DateType dateType) {
        this.dateType = dateType;
        return this;
    }


    public String getFieldName() {
        return fieldName;
    }

    public ProductStructureParam setFieldName(String fieldName) {
        this.fieldName = fieldName;
        return this;
    }

    public String getTotalName() {
        return totalName;
    }

    public ProductStructureParam setTotalName(String totalName) {
        this.totalName = totalName;
        return this;
    }

    public BigDecimal getDefaultValue() {
        return defaultValue;
    }

    public ProductStructureParam setDefaultValue(BigDecimal defaultValue) {
        this.defaultValue = defaultValue;
        return this;
    }

    public Integer getScaleNum() {
        return scaleNum;
    }

    public ProductStructureParam setScaleNum(Integer scaleNum) {
        this.scaleNum = scaleNum;
        return this;
    }

    public String getTypeName() {
        return typeName;
    }

    public ProductStructureParam setTypeName(String typeName) {
        this.typeName = typeName;
        return this;
    }


    public List<GroupField> getGroupFields() {
        return groupFields;
    }

    public ProductStructureParam setGroupFields(List<GroupField> groupFields) {
        this.groupFields = groupFields;
        return this;
    }

    public List<String> getCodes() {
        return codes;
    }

    public ProductStructureParam setCodes(List<String> codes) {
        this.codes = codes;
        return this;
    }

    public String getProductStructType() {
        return productStructType;
    }

    public ProductStructureParam setProductStructType(String productStructType) {
        this.productStructType = productStructType;
        return this;
    }

    public ProductStructIndex getProductStructIndex() {
        return productStructIndex;
    }

    public ProductStructureParam setProductStructIndex(ProductStructIndex productStructIndex) {
        this.productStructIndex = productStructIndex;
        return this;
    }
}
